package com.ruoyi.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.cms.entity.domain.Model;
import com.ruoyi.cms.entity.domain.ModelIntroduction;
import com.ruoyi.cms.mapper.ModelIntroductionMapper;
import com.ruoyi.cms.mapper.ModelMapper;
import com.ruoyi.cms.service.DashScopeService;
import com.ruoyi.cms.service.ModelIntroductionService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.RedisKeyEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ModelIntroductionServiceImpl implements ModelIntroductionService {

    @Autowired
    private ModelIntroductionMapper modelIntroductionMapper;
    @Autowired
    private DashScopeService dashScopeService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ModelMapper modelMapper;

    @Override
    public void addIntroduction(Model model) {
        GenerationResult call = dashScopeService.call(model.getName());

        ModelIntroduction modelIntroduction = new ModelIntroduction();
        modelIntroduction.setModelId(model.getId());
        modelIntroduction.setIntroduction(call.getOutput().getChoices().get(0).getMessage().getContent());

        modelIntroductionMapper.insert(modelIntroduction);

        redisCache.setCacheObject(RedisKeyEnum.CMS_MODEL_INTRODUCTION.getPrefix() + modelIntroduction.getModelId(),
                modelIntroduction.getIntroduction(),
                RedisKeyEnum.CMS_MODEL_INTRODUCTION.getTimeout(),
                RedisKeyEnum.CMS_MODEL_INTRODUCTION.getTimeUnit());
    }

    @Override
    public void updateIntroduction(Model model) {
        GenerationResult call = dashScopeService.call(model.getName());

        ModelIntroduction modelIntroduction = modelIntroductionMapper.selectOne(new LambdaQueryWrapper<ModelIntroduction>()
                .eq(ModelIntroduction::getModelId, model.getId()));

        if (null == modelIntroduction) {
            modelIntroduction = new ModelIntroduction();
            modelIntroduction.setModelId(model.getId());
            modelIntroduction.setIntroduction(call.getOutput().getChoices().get(0).getMessage().getContent());
            modelIntroductionMapper.insert(modelIntroduction);
        }

        modelIntroduction.setIntroduction(call.getOutput().getChoices().get(0).getMessage().getContent());
        modelIntroductionMapper.updateById(modelIntroduction);


        redisCache.setCacheObject(RedisKeyEnum.CMS_MODEL_INTRODUCTION.getPrefix() + modelIntroduction.getModelId(),
                modelIntroduction.getIntroduction(),
                RedisKeyEnum.CMS_MODEL_INTRODUCTION.getTimeout(),
                RedisKeyEnum.CMS_MODEL_INTRODUCTION.getTimeUnit());
    }

    @Override
    public String getIntroduction(Long modelId) {
        if (redisCache.hasKey(RedisKeyEnum.CMS_MODEL_INTRODUCTION.getPrefix() + modelId)) {
            return redisCache.getCacheObject(RedisKeyEnum.CMS_MODEL_INTRODUCTION.getPrefix() + modelId);
        }

        LambdaQueryWrapper<ModelIntroduction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ModelIntroduction::getModelId, modelId);
        ModelIntroduction modelIntroduction = modelIntroductionMapper.selectOne(queryWrapper);

        if (null == modelIntroduction) {
            modelIntroduction = new ModelIntroduction();
            modelIntroduction.setModelId(modelId);
            modelIntroductionMapper.insert(modelIntroduction);
        }

        if (StrUtil.isBlank(modelIntroduction.getIntroduction())) {
            Model model = modelMapper.selectById(modelId);
            GenerationResult call = dashScopeService.call(model.getName());
            modelIntroduction.setIntroduction(call.getOutput().getChoices().get(0).getMessage().getContent());
            modelIntroductionMapper.updateById(modelIntroduction);
        }

        redisCache.setCacheObject(RedisKeyEnum.CMS_MODEL_INTRODUCTION.getPrefix() + modelIntroduction.getModelId(),
                modelIntroduction.getIntroduction(),
                RedisKeyEnum.CMS_MODEL_INTRODUCTION.getTimeout(),
                RedisKeyEnum.CMS_MODEL_INTRODUCTION.getTimeUnit());

        return modelIntroduction.getIntroduction();
    }
}
